home *** CD-ROM | disk | FTP | other *** search
Wrap
const Ci = Components.interfaces, Cc = Components.classes, Cr = Components.results; function UnloadComponent() { } UnloadComponent.prototype = { classID: Components.ID("{a3c05366-aa2c-4e1b-9ab0-5168e50cec61}"), // xxx generate guid contractID: "@mozdev.org/bookmarkpreviews;1", // XXX generated contractid too classDescription: "Bookmark Previews Uninstall Component", QueryInterface: function(aIID) { if(!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIObserver) && !aIID.equals(Ci.nsISupportsWeakReference)) // you can claim you implement more interfaces here throw Cr.NS_ERROR_NO_INTERFACE; return this; }, _prefs : null, get prefs(){ if (!this._prefs){ this._prefs = Components.classes["@mozilla.org/preferences-service;1"]. getService(Components.interfaces.nsIPrefService); this._prefs = this._prefs.getBranch("extensions.bookmarkpreviews."); } return this._prefs; }, // nsIObserver implementation observe: function(aSubject, aTopic, aData) { //dump("bookmark previews observer "+aTopic+"\n"); switch(aTopic) { case "xpcom-startup": //dump("xpcom-startup"); // this is run very early, right after XPCOM is initialized, but before // user profile information is applied. Register ourselves as an observer // for 'quit-application'. var obsSvc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService); obsSvc.addObserver(this, "quit-application", true); break; case "quit-application": /* Check if this is being uninstalled. If so, delete the bookmarkpreviews folder. */ const PREFIX_ITEM_URI = "urn:mozilla:item:"; const PREFIX_NS_EM = "http://www.mozilla.org/2004/em-rdf#"; const OP_NEEDS_UNINSTALL = "needs-uninstall"; var itemId = "bookmarkpreviews@mozdev.org"; var rdf = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService); var itemResource = rdf.GetResource(PREFIX_ITEM_URI + itemId); if (itemResource) { var extmgr = Cc["@mozilla.org/extensions/manager;1"].getService(Ci.nsIExtensionManager); var ds = extmgr.datasource; var target = ds.GetTarget(itemResource, rdf.GetResource(PREFIX_NS_EM + "opType"), true); if (target && target instanceof Ci.nsIRDFLiteral){ if (target.Value == OP_NEEDS_UNINSTALL){ this.uninstall(); } } } break; default: throw Components.Exception("Unknown topic: " + aTopic); } }, uninstall : function(){ var dir = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties) .get("ProfD", Components.interfaces.nsIFile); dir.append("bookmarkpreviews"); if (dir.exists()){ dir.remove(true); } /* remove firstrun pref so previews will be created again if reinstalled */ this.prefs.resetBranch(""); } }; // constructors for objects we want to XPCOMify var objects = [UnloadComponent]; /* * Registration code. * */ const UNLOAD_OBSERVER_NAME = "Bookmark Previews Uninstall Observer"; function FactoryHolder(aObj) { this.CID = aObj.prototype.classID; this.contractID = aObj.prototype.contractID; this.className = aObj.prototype.classDescription; this.factory = { createInstance: function(aOuter, aIID) { if(aOuter) throw Cr.NS_ERROR_NO_AGGREGATION; return (new this.constructor).QueryInterface(aIID); } }; this.factory.constructor = aObj; } var gModule = { registerSelf: function (aComponentManager, aFileSpec, aLocation, aType) { aComponentManager.QueryInterface(Ci.nsIComponentRegistrar); for (var key in this._objects) { var obj = this._objects[key]; aComponentManager.registerFactoryLocation(obj.CID, obj.className, obj.contractID, aFileSpec, aLocation, aType); } // this can be deleted if you don't need to init on startup var catman = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); catman.addCategoryEntry("xpcom-startup", UNLOAD_OBSERVER_NAME, UnloadComponent.prototype.contractID, true, true); //catman.addCategoryEntry("xpcom-shutdown", UNLOAD_OBSERVER_NAME, // UnloadComponent.prototype.contractID, true, true); }, unregisterSelf: function(aCompMgr, aFileSpec, aLocation) { // this must be deleted if you delete the above code dealing with |catman| var catman = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); catman.deleteCategoryEntry("xpcom-startup", UNLOAD_OBSERVER_NAME, true); // end of deleteable code aComponentManager.QueryInterface(Ci.nsIComponentRegistrar); for (var key in this._objects) { var obj = this._objects[key]; aComponentManager.unregisterFactoryLocation(obj.CID, aFileSpec); } }, getClassObject: function(aComponentManager, aCID, aIID) { if (!aIID.equals(Ci.nsIFactory)) throw Cr.NS_ERROR_NOT_IMPLEMENTED; for (var key in this._objects) { if (aCID.equals(this._objects[key].CID)) return this._objects[key].factory; } throw Cr.NS_ERROR_NO_INTERFACE; }, canUnload: function(aComponentManager) { return true; }, _objects: {} //FactoryHolder }; function NSGetModule(compMgr, fileSpec) { for(var i in objects) gModule._objects[i] = new FactoryHolder(objects[i]); return gModule; }